<?php

/**
 * Implements hook_theme()
 */
function nodejschat_theme() {
  return array(
    'nodejschat' => array(
      'render element' => 'elements',
      'template' => 'nodejschat',
      'path' => drupal_get_path('module', 'nodejschat') . '/templates',
    ),
    'nodejschat_message' => array(
      'render element' => 'elements',
      'template' => 'nodejschat.message',
      'path' => drupal_get_path('module', 'nodejschat') . '/templates',
    ),
    'nodejschat_entry' => array(
      'render element' => 'elements'
    ),
    'nodejschat_message_user_picture' => array(
      'variables' => array('account' => NULL, 'picture_style' => NULL),
    ),
  );
}

/**
 * Theme a chat in the IRC style.
 */
function template_preprocess_nodejschat(&$variables) {
  $nodejschat = $variables['elements']['#nodejschat'];
  $variables['cid'] = $nodejschat->cid;
  $variables['title'] = check_plain($nodejschat->title);
  
  // Flatten the node object's member fields.
  $variables = array_merge((array) $nodejschat, $variables);

  // Helpful $content variable for templates.
  $variables += array('content' => array());
  foreach (element_children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }

  // Make the field variables available with the appropriate language.
  field_attach_preprocess('nodejschat', $nodejschat, $variables['content'], $variables);

  // Gather nodejschat classes.
  $variables['classes_array'][] = drupal_html_class('nodejschat-' . $nodejschat->cid);

  // Add additional libraries, CSS, JavaScript an other custom
  // attached data associated with this element.
  if (!empty($variables['elements']['#attached'])) {
    drupal_process_attached($variables['elements']);
  }
}

/**
 * Theme a message in the board style.
 */
function template_preprocess_nodejschat_message(&$variables) {
  $message = $variables['elements']['#message'];
  $nodejschat = $variables['elements']['#nodejschat'];
  $variables['cmid'] = $message->cmid;

  // Flatten the node object's member fields.
  $variables = array_merge((array) $message, $variables);

  // Helpful $content variable for templates.
  $variables += array('content' => array());
  foreach (element_children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }

  // Display user picture.
  if ($nodejschat->settings['user_picture']) {
    $variables['user_picture'] = theme('nodejschat_message_user_picture', array('account' => $message, 'picture_style' => $nodejschat->settings['user_picture_style']));
  }
  else {
    $variables['user_picture'] = '';
  }

  // Display post information only on certain node types.
  if ($nodejschat->settings['message_posted']) {
    $variables['display_posted'] = TRUE;
    $variables['posted'] = t('Posted by !username on !datetime', array(
      '!username' => $variables['content']['username'],
      '!datetime' => $variables['content']['datetime']
    ));
  }
  else {
    $variables['display_posted'] = FALSE;
    $variables['posted'] = '';
  }

  // Gather nodejschat classes.
  $variables['classes_array'][] = 'new-message';
}
/**
 * Theme nodejschat chat in the block style.
 */
function theme_nodejschat_entry($variables) {
  $form = $variables['elements']['entry_form'];
  $nodejschat = $form['#nodejschat'];
  $output = '<div class="nodejschat-entry" id="nodejschat-entry-' . $nodejschat->cid . '">';
  $output .= drupal_render($form);
  $output .= '</div>';
  return $output;
}

/**
 * Process variables for user-picture.tpl.php.
 *
 * The $variables array contains the following arguments:
 * - $account: A user, node or comment object with 'name', 'uid' and 'picture'
 *   fields.
 *
 * @see user-picture.tpl.php
 */
function theme_nodejschat_message_user_picture($variables) {
  $account = $variables['account'];
  $style = $variables['picture_style'];
  if (!empty($account->picture)) {
    if (is_numeric($account->picture)) {
      $account->picture = file_load($account->picture);
    }
    if (!empty($account->picture->uri)) {
      $filepath = $account->picture->uri;
    }
  }
  elseif (variable_get('user_picture_default', '')) {
    $filepath = variable_get('user_picture_default', '');
  }
  if (isset($filepath)) {
    $alt = t("@user's picture", array('@user' => format_username($account)));
    // If the image does not have a valid Drupal scheme (for eg. HTTP),
    // don't load image styles.

    if (module_exists('image') && file_valid_uri($filepath) && $style) {
      $user_picture = theme('image_style', array('style_name' => $style, 'path' => $filepath, 'alt' => $alt, 'title' => $alt));
    }
    else {
      $user_picture = theme('image', array('path' => $filepath, 'alt' => $alt, 'title' => $alt));
    }
    if (!empty($account->uid) && user_access('access user profiles')) {
      $attributes = array('attributes' => array('title' => t('View user profile.')), 'html' => TRUE);
      $user_picture = l($user_picture, "user/$account->uid", $attributes);
    }
    return '<div class="user-picture">' . $user_picture . '</div>';
  }
  return '';
}